import echarts from 'echarts';
// 引入柱状图
import 'echarts/lib/chart/bar';
// 引入提示框和标题组件
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';
import storageApi from '../../../core/storage/storage';

function deviceController($rootScope, $scope, homeStatsService) {
  'ngInject';

  // 设备进出场统计
  this.statsDeviceInout = () => {
    const enterNum = [];
    const inNum = [];
    const leaveNum = [];
    homeStatsService
      .querySelfDeviceInOutStatistics({ orgId: $scope.orgId })
      .then((res) => {
        if (res.success) {
          this.deviceNum = res.rows;
          if (this.deviceNum && this.deviceNum.length > 0) {
            this.deviceNum.forEach((item) => {
              enterNum.push(item.enterNum);
              inNum.push(item.inNum);
              leaveNum.push(item.leaveNum);
            });
            this.initCharts(enterNum, inNum, leaveNum);
          }
        }
      });
  };

  this.initCharts = (enterNum, inNum, leaveNum) => {
    const option4 = {
      color: ['#59b0e7', '#7cc78a', '#a9a9a9'],
      title: {
        text: '设备进出场情况汇总',
        x: 'center',
        textStyle: { color: '#666' },
      },
      tooltip: {
        trigger: 'axis',
      },
      legend: {
        data: ['本月进场设备数', '本月离场设备数', '本月在线设备数'],
        left: 'center',
        top: 30,
      },
      calculable: true,
      xAxis: [
        {
          type: 'category',
          data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
        },
      ],
      yAxis: [
        {
          show: false,
          type: 'value',
        },
      ],
      series: [
        {
          name: '本月进场设备数',
          type: 'bar',
          stack: '全部',
          barWidth: 30,
          itemStyle: {
            normal: {
              label: {
                show: true,
                position: 'top',
              },
            },
          },
          data: enterNum,
        },
        {
          name: '本月离场设备数',
          type: 'bar',
          stack: '全部',
          barWidth: 30,
          itemStyle: {
            normal: {
              label: {
                show: true,
                position: 'bottom',
              },
            },
          },
          data: leaveNum,
        },
        {
          name: '本月在线设备数',
          type: 'line',
          label: {
            normal: {
              show: true,
              position: 'top',
            },
          },
          data: inNum,
        },
      ],
    };
    const myChart = echarts.init(document.getElementById('device-total'));
    myChart.setOption(option4);
  };

  this.statsDeviceInout();
}

export default deviceController;
